// components/area/area.js
import service from '../../utils/request'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    title: {
      type: String,
      default: '',
    },
    type:{
      type: Number,
      default: 2
    } ,
    isRequired: {
      type: Boolean,
      default: false
    },
    oneValue: {
      type: Number,
      default: 0
    },
    secondValue: {
      type: Number,
      default: 0
    },
    threeValue: {
      type: Number,
      default: 0
    }
  },
  observers: {
    'oneValue': function(one) {
      if(this.data.freshTime){
        console.log('省')
        this.getProvince()
      }
      this.getCity()
    },
    'secondValue': function(second) {
      this.getArea()
    },
    // 'threeValue': function(three) {
    //   this.getArea()
    // },
  },

  /**
   * 组件的初始数据
   */
  data: {
    addressCity:[],
    multiArray: [],  // 三维数组数据
    multiIndex: [0, 0, 0], // 默认的下标,
    selectProvinceId:null,
    selectCityId:null,
    currentCityId: '',
    currentProvinceId: '',
    selectAreaId:null,
    allData: [],
    province_list:null,
    province_name:null,
    city_list:null,
    city_name:null,
    area_list:null,
    area_name:null,
    freshTime: true,
  },
  created() {
    // this.getProvince()
  },
  /**
   * 组件的方法列表
   */
  methods: {
    getProvince() {
      let that = this
      
      
      service('POST', '/devops/power/box/device/list/all',{pid: 0}).then((res) => {
        console.log('变压器', res.data.list, that.data.type)
        // if(that.data.type !=4){
          // res.data.list.push({id: -99, name: '请选择'})
          let result = [{id: -99, name: '请选择'}, ...res.data.list]
        // }
        let provinceList = result  //放到一个数组里面
        let provinceArr = result.map((item)=>{return item.name})  //获取名称
        //判断1级id的数组下标和名称
        let cur='', i = ''
        let value = that.data.oneValue
        if(value){
          result.map((item, index)=>{
            if(value == item.id){
              cur = item
              i = index
            }
          })
        }
        console.log('cur-one', cur, i,value)
        if(cur){
          that.setData({
            "multiIndex[0]": i,
            "addressCity[0]":  cur.name,
            freshTime: false,
          })
        }
        console.log('provinceArr', provinceArr)
        let  multiArray = [provinceArr,[],[]]
        if(that.data.type==2){
          multiArray = [provinceArr,[]]
        }
        that.setData({
          multiArray:multiArray, //更新三维数组  更新完为[['广东','北京'],[],[]]
          province_list:provinceList,  //省级原始数据
          province_name:provinceArr,  //省级全部名称
        })
        let defaultCode = that.data.province_list[0].id //使用第一项当作参数获取市级数据
        if(defaultCode){
          that.setData({
            // currnetProvinceId:defaultCode  //保存当前省份id
          })
          // that.getCity(defaultCode) //获取市区数据
        }
      })
    },
    getCity(pids=0, res = true, defaultId= 0){
      let that = this
      if(!this.data.freshTime){
        console.log('清除数据')
        that.setData({
          multiArray: [this.data.province_name, [], []],
          currentCityId: this.data.secondValue,
          secondValue: -99,
          city_list: [],
          area_list: [],
          area_name: [],
          city_name: [],
          three: -99,
          
        })
      }
      let pid = this.data.oneValue
      if(!pid || pid ==-99){
        return
      }
      
        service('POST', `/devops/power/box/device/child/${pid}`,{
        }).then((res) => {
          console.log('获取二级数据',res.data)
          let result = [{id: -99, name: '请选择'}]
          // if(res.data.info.childs && that.data.type !=4){
            result = [...result, ...res.data.info.childs]
          // }else if(!res.data.info.childs){
          // }else{
          //   result = res.data.info.childs
          // }
          // console.log('二级配电箱加上请选择', result)
          let cityArr =result.map((item)=>{return item.name}) //返回城市名称
          let cityList = result
          let  multiArray = [that.data.province_name,cityArr,[]]
          if(that.data.type==2){
            multiArray = [that.data.province_name,cityArr, that.data.area_name]
          }
          //判断2级id的数组下标和名称
          let cur='', i = ''
          let value = that.data.secondValue
          if(value){
            result.map((item, index)=>{
              if(value == item.id){
                cur = item
                i = index
              }
            })
          }
          console.log('cur22', cur, i, value)
          if(cur){
            that.setData({
              "multiIndex[1]": i,
              "addressCity[1]":  cur.name
            })
          }
          that.setData({
            multiArray:multiArray,  //更新后[['广东','北京'],['潮州'，'汕头','揭阳'],[]]
            city_list:cityList, //保持市级数据
            city_name:cityArr   //市级名称
          })
          let defaultCode = that.data.city_list[defaultId].id //获取第一个市的区级数据
          console.log('cityList',cityList)
          if(defaultCode){
            that.setData({
              // currentCityId:defaultCode //保存当下市id
            })
            if(that.data.type !=2){
              let data = cityList[defaultId].childs? cityList[defaultId].childs: []
              // console.log('data',cityList)
              that.getArea(defaultCode, data) //获取区域数据
            }
            
          }
        })
      
    },
    getArea(pids= 0, cityLists = []){
      let that = this
      let pid = this.data.secondValue
      if(!pid || pid ==-99){return }
      // that.setData({
      //   selectAreaId: this.data.threeValue
      // })
      service('POST', `/devops/power/box/device/child/${pid}`,{
      }).then((res) => {
        let result = [{id: -99, name: '请选择'}]
          // if(res.data.info.childs && that.data.type !=4){
            result = [...result, ...res.data.info.childs]
          // }else if(!res.data.info.childs){
          // }else{
          //   result = res.data.info.childs
          // }
          console.log('最终三级数据', res)
          let areaList = result
          let areaArr = result.map((item)=>{return item.name}) //区域名
          //判断3级id的数组下标和名称
          let cur='', i = ''
          let value = that.data.threeValue
          if(value){
            result.map((item, index)=>{
              if(value == item.id){
                cur = item
                i = index
              }
            })
          }
       console.log('cur3', cur, i, )
       if(cur){
         that.setData({
           "multiIndex[2]": i,
           "addressCity[2]":  cur.name
         })
       }
      let mul = [that.data.province_name,that.data.city_name,areaArr]
      // console.log('三级配电箱1',pid,  mul)
      console.log('三级名称', areaArr)
      // return
      this.setData({
        multiArray:[that.data.province_name,that.data.city_name,areaArr],
        area_list:areaList, //区列表
        area_name:areaArr   //区名字
      })
      })
      return
      // console.log('三级配电箱', cityList)
      let result = [{id: -99, name: '请选择'}]
      if(cityList &&  that.data.type !=4){
       result = [{id: -99, name: '请选择'}, ...cityList]
      }else{
        result = cityList
      }
      that.setData({
        currentCityId: pid
      })
      

      console.log('data', that.data)
    },


    //picker确认选择地区
  bindRegionChange:function(e){
    console.log('bindRegionChange',{one:this.data.oneValue, two: this.data.secondValue, three: this.data.threeValue})
    // if((e.detail.value[1]==null || e.detail.value[2]==null) && this.data.type !=2 ){ 
    //   console.log('如果只滚动了第一列则选取第一列的第一数据')
    //   let data = [this.data.province_list[e.detail.value[0]].name, this.data.city_list[0].name]
    //   var selectAreaId = -99
    //   if(this.data.type !=2){
    //     data = [this.data.province_list[e.detail.value[0]].name, this.data.city_list[0].name, this.data.area_list[0].name]
    //     selectAreaId = this.data.area_list[0].id
    //   }
    //   this.setData({
    //     multiIndex: e.detail.value, 
    //     addressCity: data,
    //     selectProvinceId: this.data.province_list[e.detail.value[0]].id,
    //     selectCityId: this.data.city_list[0].id,
    //     selectAreaId: selectAreaId
    //   })
    // }
    // else{
    //   let data = [this.data.province_list[e.detail.value[0]].name, this.data.city_list[e.detail.value[1]].name]
    //   var selectAreaId = -99
    //   if(this.data.type !=2){
    //     data = [this.data.province_list[e.detail.value[0]].name, this.data.city_list[e.detail.value[1]].name, this.data.area_list[e.detail.value[2]].name]
    //     selectAreaId = this.data.area_list[e.detail.value[2]].id
    //   }
    //   this.setData({
    //     multiIndex: e.detail.value,  
    //     addressCity: data,
    //     selectProvinceId: this.data.province_list[e.detail.value[0]].id,
    //     selectCityId: this.data.city_list[e.detail.value[1]].id,
    //     selectAreaId: selectAreaId
    //   })
    // }
    this.triggerEvent('confirmData', {one:this.data.oneValue, two: this.data.secondValue, three: this.data.threeValue})
  },
      //滑动地区组件
  bindRegionColumnChange:function(e){
    console.log('当前改变列-值',e.detail.column,e.detail.value, this.data.addressCity)
    let that = this
    let column = e.detail.column  //当前改变的列
    let data = {
      multiIndex:that.data.multiIndex,
      multiArray:that.data.multiArray,
      addressCity:that.data.addressCity,
    }
    data.multiIndex[column] = e.detail.value  //第几列改变了就是对应multiIndex的第几个，更新他
    switch(column){
      case 0 :  //第一列改变，省级改变
      var currentProvinceId = that.data.province_list[e.detail.value].id
        if(currentProvinceId != that.data.oneValue){ //判断当前id是不是更新了
          data.oneValue = currentProvinceId
          data.secondValue = -99
          data.threeValue = -99
          data.addressCity[0] = that.data.province_list[e.detail.value].name
          // that.getCity(currentProvinceId)   //获取当前id下的市级数据
        }
        data.multiIndex[1] = 0    //将市默认选择第一个
        data.multiIndex[2] = 0
        break
      case 1 :  //第二列改变，市级改变
        let currentCityId = that.data.city_list[e.detail.value].id
        if(currentCityId != that.data.secondValue){
          data.secondValue = currentCityId
          data.threeValue = -99
          data.addressCity[1] = that.data.city_list[e.detail.value].name
          // that.setData({
          //   secondValue: currentCityId,
          // })
        }
        data.multiIndex[2] = 0  //区域默认第一个
        break
        case 2 :
          let currentAreaId = that.data.area_list[e.detail.value].id
          data.addressCity[2] = that.data.area_list[e.detail.value].name
          data.threeValue = currentAreaId
    }
    that.setData(data)  //更新数据
  },
  }
})
